package main

import (
	"bufio"
	"fmt"
	"net"
	"os"

	"github.com/wangtuanjie/ip17mon"

	"golang/findip"
	"strings"
)

func main() {
	// t()
	// scanIP()

	// tParseIP()

	// tlen()
	// s := "asdf"
	// tstackString(s)

	// tnils()
	t()
	scanIP()
}

func tstackString(s string) (a int) {
	p()
	return
}

func p() {
	ss := []byte("asdf")
	fmt.Println(ss[33])
}

func tlen() {
	s := "abc\n"
	fmt.Println(len(s), "->", s)
	s = s[:len(s)-1]
	fmt.Println(len(s), "->", s)
}

func tParseIP() {
	ip := net.ParseIP("2031:0000:1F1F:0000:0000:0100:11A0:ADDF")
	fmt.Println(ip.String())
	ip17mon.Find(ip.String())
}

func scanIP() {
	scanner := bufio.NewScanner(os.Stdin)

	stdw := bufio.NewWriter(os.Stdout)

	for scanner.Scan() {
		rawline := scanner.Text()

		bindex := strings.Index(rawline, `ip":`) + 5
		gindex := bindex
		for {
			if rawline[gindex] == '"' {
				break
			}
			gindex++
		}
		rawip := rawline[bindex:gindex]
		ip := net.ParseIP(rawip)
		ip = ip.To4()
		if ip != nil {
			loc, _ := findip.Find(ip.String())
			if loc.Region == `俄罗斯` {
				stdw.WriteString(rawip + "\n")
			}
		}
	}
	stdw.Flush()
}

func t() {
	findip.Init("./17monipdb.dat")
}

func tnils() {
	var a ttt
	a.gaga()
}

type ttt []byte

func (ttt) gaga() {

}
